const db = require('./db')

// 封装查询数据库的方法
function sqlExcute(sql, values) {
  return new Promise((resolve, reject) => {
    db.query(sql, values, (err, results, fields) => {
      if (err) reject(err)
      resolve(results)
    })
  })
}

// 条件查询
const getSearchSql = (searchObj) => {
  let sql = ''
  for (let key in searchObj) {
    sql += ` AND ${key} LIKE '%${searchObj[key]}%'`
  }
  return sql
}

module.exports = {
  sqlExcute,
  getSearchSql
}
